import { httpClient } from '@/utils/http/axios';
import { ErrorMessageMode } from '#/axios';
import { AjaxResult } from '@/api/model';
import {
  DeviceState,
  DeviceInfo,
  DeviceVariableResponse,
  DeviceVariableIdentity,
  DeviceVariableInfo,
} from './model';

/** IoT枚举 */
enum Api {
  /** 获取设备状态集合 */
  GetDeviceStates = '/api/iot/getDeviceStates',
  /** 获取设备状态 */
  GetDeviceState = '/api/iot/getDeviceState',

  /** 获取设备集合 */
  GetDevices = '/api/iot/getDevices',
  /** 获取设备 */
  GetDevice = '/api/iot/getDevice',

  /** 根据名称获取设备变量 */
  GetDeviceVariableByName = '/api/iot/getDeviceVariableByName',
  /** 读取设备变量 */
  GetDeviceVariableByIdentity = '/api/iot/getDeviceVariableByIdentity',

  /** 写入设备变量 */
  WriteDeviceVariable = '/api/iot/writeDeviceVariable',
}

//#region 获取设备状态集合
/**
 * 获取设备状态集合
 * @param mode 错误消息模型
 */
export function getDeviceStates(mode: ErrorMessageMode = 'message') {
  return httpClient.get<DeviceState[]>(
    {
      url: Api.GetDeviceStates,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 获取设备状态
/**
 * 获取设备状态
 * @param deviceNames 设备名称
 * @param mode 错误消息模型
 */
export function getDeviceState(deviceNames: string[], mode: ErrorMessageMode = 'message') {
  return httpClient.post<DeviceState[]>(
    {
      url: Api.GetDeviceState,
      params: deviceNames,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 获取设备集合
/**
 * 获取设备集合
 * @param mode 错误消息模型
 */
export function getDevices(mode: ErrorMessageMode = 'message') {
  return httpClient.get<DeviceInfo[]>(
    {
      url: Api.GetDevices,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 获取设备
/**
 * 获取设备
 * @param deviceNames 设备名称
 * @param mode 错误消息模型
 */
export function getDevice(deviceNames: string[], mode: ErrorMessageMode = 'message') {
  return httpClient.post<DeviceInfo[]>(
    {
      url: Api.GetDevice,
      params: deviceNames,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 根据名称获取设备变量
/**
 * 根据名称获取设备变量
 * @param deviceNames 设备名称
 * @param mode 错误消息模型
 */
export function getDeviceVariableByName(deviceNames: string[], mode: ErrorMessageMode = 'message') {
  return httpClient.post<DeviceVariableResponse[]>(
    {
      url: Api.GetDeviceVariableByName,
      params: deviceNames,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 根据标识获取设备变量
/**
 * 根据标识获取设备变量
 * @param dtos 设备变量标识
 * @param mode 错误消息模型
 */
export function getDeviceVariableByIdentity(
  dtos: DeviceVariableIdentity[],
  mode: ErrorMessageMode = 'message',
) {
  return httpClient.post<DeviceVariableResponse[]>(
    {
      url: Api.GetDeviceVariableByIdentity,
      params: dtos,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 写入设备变量
/**
 * 写入设备变量
 * @param dtos 设备变量信息
 * @param mode 错误消息模型
 */
export function writeDeviceVariable(
  dtos: DeviceVariableInfo[],
  mode: ErrorMessageMode = 'message',
) {
  return httpClient.post<AjaxResult>(
    {
      url: Api.WriteDeviceVariable,
      params: dtos,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
